package com.sinochem.yunlian.ship.base.rest;

import com.alibaba.fastjson.JSONObject;
import com.sinochem.yunlian.ship.base.biz.BsCategoryBiz;
import com.sinochem.yunlian.ship.base.entity.BsCategory;
import com.sinochem.yunlian.ship.base.vo.request.ReqCategoryVo;
import com.sinochem.yunlian.ship.base.vo.response.CategoryVo;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import com.sinochem.yunlian.ship.common.constant.RedisKey;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cyp
 * @date 2017-11-20 21:02
 *
 * */
@IgnoreToken
@RestController
@RequestMapping("/anon/category")
public class BsCategoryController extends BaseController<BsCategoryBiz,BsCategory> {

    @Autowired
    private BsCategoryBiz bsCategoryBiz;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查找类别字典列表
     * */
    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public RestResponse findAll(@RequestBody ReqCategoryVo reqCategoryVo){
        RestResponse result = new RestResponse();
        List<CategoryVo> listVo = new ArrayList<>();
        String categoryLevelKey = String.format(RedisKey.KEY_CATEGORY_LEVEL_PREFIX , reqCategoryVo.getCategoryType());
        //查询所有已经启用的货品种类信息 目前只有两级后期改为递归
        if (redisTemplate.hasKey(categoryLevelKey)) {
            String allCategory = (String) redisTemplate.opsForValue().get(categoryLevelKey);
            listVo = JSONObject.parseArray(allCategory,CategoryVo.class);
        } else {
            List<BsCategory> list = bsCategoryBiz.findAllCategory(reqCategoryVo.getCategoryType());
            for (BsCategory category : list) {
                if (category.getLevel() == 1) {
                    CategoryVo vo = transformEntity2Vo(category);
                    listVo.add(vo);
                    for (BsCategory categoryTwo : list) {
                        if (categoryTwo.getLevel() == 2 && categoryTwo.getParentId().equals(vo.getId())) {
                            if (vo.getChildrens() == null) {
                                vo.setChildrens(new ArrayList<>());
                            }
                            CategoryVo voTwo = transformEntity2Vo(categoryTwo);
                            vo.getChildrens().add(voTwo);
                            for (BsCategory categoryThree : list) {
                                if (categoryThree.getLevel() == 3 && categoryThree.getParentId().equals(categoryTwo.getId())) {
                                    if (voTwo.getChildrens() == null) {
                                        voTwo.setChildrens(new ArrayList<>());
                                    }
                                    CategoryVo voThree = transformEntity2Vo(categoryThree);
                                    voTwo.getChildrens().add(voThree);
                                }
                            }
                        }
                    }
                }
            }
            redisTemplate.opsForValue().set(categoryLevelKey,JSONObject.toJSONString(listVo));
        }
        result.setData(listVo);
        return result;
    }

    @RequestMapping(value = "/findByLevel", method = RequestMethod.POST)
    public RestResponse findByLevel(@RequestBody ReqCategoryVo reqCategoryVo){
        RestResponse result = new RestResponse();
        List<CategoryVo> listVo = new ArrayList<>();
        String categoryLevelKey = String.format(RedisKey.KEY_CATEGORY_LEVEL_PREFIX,reqCategoryVo.getCategoryType());
        //查询所有已经启用的货品种类信息 目前只有两级后期改为递归
        if (redisTemplate.hasKey(categoryLevelKey)) {
            String allCategory = (String) redisTemplate.opsForValue().get(categoryLevelKey);
            listVo = JSONObject.parseArray(allCategory,CategoryVo.class);
        } else {
            List<BsCategory> list = bsCategoryBiz.findByLevel();
            for (BsCategory category : list) {
                if (category.getLevel() == 2) {
                    CategoryVo vo = transformEntity2Vo(category);
                    listVo.add(vo);
                    for (BsCategory categoryOne : list) {
                        if (categoryOne.getLevel() == 3 && categoryOne.getParentId().equals(vo.getId())) {
                            if (vo.getChildrens() == null) {
                                vo.setChildrens(new ArrayList<>());
                            }
                            CategoryVo voOne = transformEntity2Vo(categoryOne);
                            vo.getChildrens().add(voOne);
                        }
                    }
                }
            }
            redisTemplate.opsForValue().set(categoryLevelKey,JSONObject.toJSONString(listVo));
        }
        result.setData(listVo);
        return result;
    }

    public CategoryVo transformEntity2Vo (BsCategory bsCategory) {
        CategoryVo vo =  new CategoryVo();
        vo.setId(bsCategory.getId());
        vo.setCategoryName(bsCategory.getCategoryName());
        vo.setEnName(bsCategory.getEnName());
        vo.setLevel(bsCategory.getLevel());
        vo.setParentId(bsCategory.getParentId());
        vo.setSeqNum(bsCategory.getSeqNum());
        vo.setBusiId(bsCategory.getBusiId());
        return vo;
    }

}